package com.example.snackfood.services.impl;

import com.example.snackfood.dao.SnackFoodOrdersDao;
import com.example.snackfood.entity.Carts;
import com.example.snackfood.entity.Orders;
import com.example.snackfood.services.IsnackFoodOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

/**
 * @program: snackFood
 * @description:
 * @author: lydms
 * @create: 2023-11-24 22:53
 **/

@Service
public class SnackFoodOrdersService implements IsnackFoodOrdersService {

    @Autowired
    private SnackFoodOrdersDao snackFoodOrdersDao;

    @Override
    public void saveOrders(Orders orders) {
        snackFoodOrdersDao.save(orders);
    }

    @Override
    public void deleteOrders(Long id) {
        Query query=new Query(Criteria.where("id").is(id));
        snackFoodOrdersDao.delete(query);
    }


    public void updateOrders(Orders orders) {
        Query query=new Query(Criteria.where("id").is(orders.getId()));
        Update update=new Update();
        update.set("user_id",orders.getUser_id());
        update.set("order_date",orders.getOrder_date());
        update.set("delivery_date",orders.getDelivery_date());
        update.set("status",orders.getStatus());
        update.set("address",orders.getAddress());
        update.set("items",orders.getItems());
        snackFoodOrdersDao.update(query,update);
    }


    public Orders getOrders(Orders orders) {
        Criteria criteriaid=Criteria.where("id").is(orders.getId());
        Criteria orCriteria = new Criteria();
        orCriteria.orOperator(criteriaid);
        Query query=new Query(orCriteria);
        return  snackFoodOrdersDao.get(query);
    }

}
